package com.sxsihe.heartmate;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import cn.pedant.SweetAlert.widget.SweetAlertDialog;
import com.edmodo.rangebar.RangeBar;
import com.sxsihe.heartmate.util.DataManager;
import com.sxsihe.heartmate.util.FileStates;
import com.sxsihe.heartmate.util.SharePreferenceUtil;
import com.sxsihe.heartmate.view.ColorPickerDialog;
import org.w3c.dom.Text;

public class SettingsView extends FrameLayout implements View.OnClickListener {
    RangeBar rangeBar;
    String rateMin,rateMax;
    TextView rateMin_text, rateMax_text, text_color_text, line_color_text, coordinate_color_text, bg_color_text, cache_size, data_size,background_color;
    GradientDrawable drawable_text,drawable_line,drawable_coordinate,drawable_bg;
    EditText ip, port;
    RelativeLayout bg_color_layout, coordinate_color_layout, line_color_layout, text_color_layout, default_color_layout,
            cache_layout, logout_layout, data_size_layout, version_layout,about_layout;
    ToggleButton toggleButton;
    CheckBox is_alarm;
    ColorPickerDialog dialog;
    SharePreferenceUtil spu;
    Button default_color, server_confirm;
    RangeBar range_bar;

    int default_color_bg = Color.rgb(255, 255, 255);
    int default_color_text = Color.rgb(141, 141, 141);
    int default_color_line = Color.rgb(254, 2, 2);
    int default_color_coordinate = Color.rgb(230, 230, 230);

    int color_bg,color_text,color_line,color_coordinate;

    public SettingsView(Context context) {
        super(context);
        if (spu == null) {
            spu = SharePreferenceUtil.getInstance(context);
        }
        color_bg = spu.getColor("color_bg",default_color_bg);
        if(color_bg==default_color_bg){
            spu.setColor("color_bg",default_color_bg);
        }
        color_text = spu.getColor("color_text",default_color_text);
        if(color_text==default_color_text){
            spu.setColor("color_text",default_color_text);
        }
        color_line = spu.getColor("color_line",default_color_line);
        if(color_line==default_color_line){
            spu.setColor("color_line",default_color_line);
        }
        color_coordinate = spu.getColor("color_coordinate",default_color_coordinate);
        if(color_coordinate==default_color_coordinate){
            spu.setColor("color_coordinate",default_color_coordinate);
        }

        View view = LayoutInflater.from(context).inflate(R.layout.main_tab_settings, this);

        bg_color_text = (TextView)view.findViewById(R.id.bg_color_text);
        drawable_bg = (GradientDrawable)bg_color_text.getBackground();
        drawable_bg.setColor(color_bg);
        text_color_text = (TextView)view.findViewById(R.id.text_color_text);
        drawable_text = (GradientDrawable)text_color_text.getBackground();
        drawable_text.setColor(color_text);
        line_color_text = (TextView)view.findViewById(R.id.line_color_text);
        drawable_line = (GradientDrawable)line_color_text.getBackground();
        drawable_line.setColor(color_line);
        coordinate_color_text = (TextView)view.findViewById(R.id.coordinate_color_text);
        drawable_coordinate = (GradientDrawable)coordinate_color_text.getBackground();
        drawable_coordinate.setColor(color_coordinate);

        bg_color_layout = (RelativeLayout)findViewById(R.id.bg_color_layout);
        bg_color_layout.setOnClickListener(this);
        coordinate_color_layout = (RelativeLayout)findViewById(R.id.coordinate_color_layout);
        coordinate_color_layout.setOnClickListener(this);
        line_color_layout = (RelativeLayout)findViewById(R.id.line_color_layout);
        line_color_layout.setOnClickListener(this);
        text_color_layout = (RelativeLayout)findViewById(R.id.text_color_layout);
        text_color_layout.setOnClickListener(this);
//        check_update_layout = (RelativeLayout)findViewById(R.id.check_update_layout);
//        check_update_layout.setOnClickListener(this);

        default_color = (Button)findViewById(R.id.default_color);
        default_color.setOnClickListener(this);

        rateMin_text = (TextView)findViewById(R.id.rateMin);
        rateMax_text = (TextView)findViewById(R.id.rateMax);
        rateMin = spu.get("rateMin", "60");
        rateMax = spu.get("rateMax", "120");
        rateMin_text.setText("最小值"+rateMin);
        rateMax_text.setText("最大值"+rateMax);

        range_bar =(RangeBar)findViewById(R.id.range_bar);
        range_bar.setThumbIndices(Integer.parseInt(rateMin) , Integer.parseInt(rateMax) );
        range_bar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListener() {
            @Override
            public void onIndexChangeListener(RangeBar rangeBar, int leftThumbIndex, int rightThumbIndex) {
                int leftIndex = leftThumbIndex;
                int rightIndex = rightThumbIndex;
                rateMin_text.setText("最小值" + leftIndex);
                spu.set("rateMin", leftIndex + "");
                rateMax_text.setText("最大值" + rightIndex);
                spu.set("rateMax", rightIndex + "");
            }
        });

        is_alarm = (CheckBox)findViewById(R.id.is_alarm);
        is_alarm.setChecked(Boolean.valueOf(spu.get("is_alarm", "true")));
        if (Boolean.valueOf(spu.get("is_alarm","true"))) {
            rateMin_text.setTextColor(getResources().getColor(R.color.text_enabled_color));
            rateMax_text.setTextColor(getResources().getColor(R.color.text_enabled_color));
        } else {
            rateMin_text.setTextColor(getResources().getColor(R.color.text_disabled_color));
            rateMax_text.setTextColor(getResources().getColor(R.color.text_disabled_color));
        }
        is_alarm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                range_bar.setEnabled(isChecked);
                spu.set("is_alarm", isChecked + "");
                if (isChecked) {
                    rateMin_text.setTextColor(getResources().getColor(R.color.text_enabled_color));
                    rateMax_text.setTextColor(getResources().getColor(R.color.text_enabled_color));
                } else {
                    rateMin_text.setTextColor(getResources().getColor(R.color.text_disabled_color));
                    rateMax_text.setTextColor(getResources().getColor(R.color.text_disabled_color));
                }
            }
        });

        cache_layout = (RelativeLayout)findViewById(R.id.cache_layout);
        cache_layout.setOnClickListener(this);

//        about_layout = (RelativeLayout)findViewById(R.id.about_layout);
//        about_layout.setOnClickListener(this);
    }

    public void onLayout(boolean changed, int w, int h, int oldw, int oldh) {
        super.onLayout(changed, w, h, oldw, oldh);
        setDataSize();
    }

    public void setDataSize() {
//        try {
//            data_size.setText(DataManager.getCacheSize(this.getContext().getFilesDir()) + "/" + DataManager.getAvailableInternalMemorySize());
//        } catch (Exception e) {
//            data_size.setText("无法获取");
//            e.printStackTrace();
//        }
    }

    TextView current_view;

    private void startActivity(String item,int color){
        Intent intent = new Intent(SettingsView.this.getContext(),ColorChooseActivity.class);
        intent.putExtra("color", color);
        intent.putExtra("item", item);
        ((Activity)SettingsView.this.getContext()).startActivityForResult(intent, MainActivity.COLOR_CHOOSE);
        ((Activity)SettingsView.this.getContext()).overridePendingTransition(R.anim.in_right, R.anim.out_left);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.text_color_layout:
                startActivity("text",spu.getColor("color_text",default_color_text));
                break;
            case R.id.bg_color_layout:
                startActivity("bg",spu.getColor("color_bg",default_color_bg));
                break;
            case R.id.coordinate_color_layout:
                startActivity("coord",spu.getColor("color_coordinate",default_color_coordinate));
                break;
            case R.id.line_color_layout:
                startActivity("line",spu.getColor("color_line",default_color_line));
                break;

            case R.id.default_color:
                setDefaultColor();
                break;
            case R.id.cache_layout:
                final AlertDialog myDialog = new AlertDialog.Builder(getContext()).create();
                myDialog.show();
                myDialog.getWindow().setContentView(R.layout.clear_cache);
                myDialog.getWindow()
                        .findViewById(R.id.cancel)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                myDialog.dismiss();
                            }
                        });
                myDialog.getWindow()
                        .findViewById(R.id.confirm)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                spu.clear();
                                setDefaultColor();
                                DataManager.cleanFiles(getContext());
                                FileStates.changed = true;
                                myDialog.dismiss();
                            }
                        });
//                new SweetAlertDialog(SettingsView.this.getContext(), SweetAlertDialog.WARNING_TYPE)
//                    .setTitleText("清除用户信息和相关设置？")
//                    .setCancelText("不，保留")
//                    .setConfirmText("是，清除")
//                    .showCancelButton(true)
//                    .setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
//                        @Override
//                        public void onClick(SweetAlertDialog sDialog) {
//                            sDialog.dismiss();
//                        }
//                    })
//                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
//                        @Override
//                        public void onClick(SweetAlertDialog sDialog) {
//                            spu.clear();
//                            setDefaultColor();
//                            sDialog.setTitleText("成功")
//                                    .setContentText("所有用户数据清除完毕,请重新进入软件")
//                                    .setConfirmText("OK")
//                                    .showCancelButton(false)
//                                    .setCancelClickListener(null)
//                                    .setConfirmClickListener(null)
//                                    .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
//                        }
//                    })
//                    .show();
                break;
//            case R.id.about_layout:
//                Intent intent = new Intent(SettingsView.this.getContext(),AboutActivity.class);
//                ((Activity)SettingsView.this.getContext()).startActivity(intent);
//                int version = Integer.valueOf(android.os.Build.VERSION.SDK);
//                if(version  >= 5) {
//                    ((Activity)SettingsView.this.getContext()).overridePendingTransition(R.anim.in_right, R.anim.out_left);
//                }
//                break;
        }
    }

    public void setColor(String item,int color){
        if(item.equals("bg")){
            GradientDrawable bgShape4 = (GradientDrawable)bg_color_text.getBackground();
            bgShape4.setColor(Color.argb(255, Color.red(color), Color.green(color), Color.blue(color)));
            spu.setColor("color_bg",color);

        }else if(item.equals("line")){
            GradientDrawable bgShape4 = (GradientDrawable)line_color_text.getBackground();
            bgShape4.setColor(Color.argb(255,Color.red(color),Color.green(color),Color.blue(color)));
            spu.setColor("color_line",color);

        }else if(item.equals("coord")){
            GradientDrawable bgShape4 = (GradientDrawable)coordinate_color_text.getBackground();
            bgShape4.setColor(Color.argb(255,Color.red(color),Color.green(color),Color.blue(color)));
            spu.setColor("color_coordinate",color);

        }else if(item.equals("text")){
            GradientDrawable bgShape4 = (GradientDrawable)text_color_text.getBackground();
            bgShape4.setColor(Color.argb(255,Color.red(color),Color.green(color),Color.blue(color)));
            spu.setColor("color_text",color);
        }
    }

    public void setDefaultColor() {
        spu.setColor("color_bg", default_color_bg);
        spu.setColor("color_line", default_color_line);
        spu.setColor("color_coordinate", default_color_coordinate);
        spu.setColor("color_text", default_color_text);
        drawable_bg.setColor(spu.getColor("color_bg",default_color_bg));
        drawable_line.setColor(spu.getColor("color_line",default_color_line));
        drawable_text.setColor(spu.getColor("color_text",default_color_text));
        drawable_coordinate.setColor(spu.getColor("color_coordinate",default_color_coordinate));
    }
}
